<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if ($req->id_number == '') {
      echo 'ID Number is empty';
      return;
    }
    
    if ($req->first_name == '') {
      echo 'First Name is empty';
      return;
    }
    
    if ($req->last_name == '') {
      echo 'Last Name is empty';
      return;
    }
    
    if ($req->date_of_birth == '0000-00-00') {
      echo 'Date of Birth is empty';
      return;
    }
    
    $opts = new stdClass();
    $opts->cd = 't.first_name = :v1 AND t.last_name = :v2 AND t.date_of_birth = :v3';
    $opts->cdv = array(':v1' => $req->first_name, ':v2' => $req->last_name, ':v3' => $req->date_of_birth);
    $rslt = $orm->get('pb_colg_student', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($rslt) > 0 && $rslt[0]->id != $req->id) {
      echo 'Student exists already';
      return;
    }
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_colg_student', $req);
      
      $orm->exec("UPDATE pb_colg_enrollment SET student_title__ = '$req->title__' WHERE student_id = '$req->id'");
      $orm->exec("UPDATE pb_colg_invoice SET student_title__ = '$req->title__' WHERE student_id = '$req->id'");
      $orm->exec("UPDATE pb_colg_payment SET student_title__ = '$req->title__' WHERE student_id = '$req->id'");
    } else {
      $orm->add('pb_colg_student', $req);
      $req->id = $orm->last_id();
    }
    
    if (isset($req->__pg)) {
      $req->__objs = $orm->get('pb_colg_student', 't.id_number', 'DESC', $req->__pg, SIZE, NULL);
    }
    
    echo json_encode($req);
  }
  
  public static function get_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res = $orm->get('pb_colg_student', NULL, NULL, NULL, NULL, $opts)->d;
    
    $user = json_decode($_SESSION['admin_user']);
    if ($user->role == UT_MANAGEMENT) {
      $opts = new stdClass();
      $opts->cd = 't.student_id = :v1';
      $opts->cdv = array(':v1' => $req->id);
      $res[0]->__enrls = $orm->get('pb_colg_enrollment', NULL, NULL, NULL, NULL, $opts)->d;
    } else {
      $opts = new stdClass();
      $opts->cd = 't.student_id = :v1 AND t.user_id = :v2';
      $opts->cdv = array(':v1' => $req->id, ':v2' => $user->id);
      $res[0]->__enrls = $orm->get('pb_colg_enrollment', NULL, NULL, NULL, NULL, $opts)->d;
    }
    
    echo json_encode($res);
  }
  
  public static function new_obj($orm, $sys, $inst, $is_iframe) {
    $id_number = 0;
    $opts = new stdClass();
    $opts->sel = 't.id_number';
    $rslt = $orm->get('pb_colg_student', 't.id_number', 'DESC', 1, 1, $opts)->d;
    if (count($rslt) > 0) {
      $id_number = $rslt[0]->id_number + 1;
    }
    echo json_encode(array($id_number));
  }
  
  public static function submit_enrl($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_colg_enrollment', $req);
    } else {
      $req->track_id = '#' . Utils::unique(8);
      $orm->add('pb_colg_enrollment', $req);
      $req->id = $orm->last_id();
    }
    
    echo json_encode($req);
  }
  
  public static function submit_enrl_program($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_colg_enrolled_program', $req);
    } else {
      $orm->add('pb_colg_enrolled_program', $req);
      $req->id = $orm->last_id();
    }
    
    echo json_encode($req);
  }
  
  public static function get_payments($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('req', 0);
    
    $opts = new stdClass();
    $opts->cd = 't.invoice_id = :v1';
    $opts->cdv = array(':v1' => $req);
    $res = $orm->get('pb_colg_payment', NULL, NULL, NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function get_intakes($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.type = :v1';
    $opts->cdv = array(':v1' => $req->type);
    $res = $orm->get('pb_colg_intake', 't.start', 'DESC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function get_classes($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.intake_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $opts->sel = 't.*, t1.start AS __start, t1.end AS __end';
    $opts->jt = 'LEFT JOIN pb_colg_intake AS t1 ON t1.id = t.intake_id';
    $res = $orm->get('pb_colg_class', 't.course_name', 'ASC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function get_classes2($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.intake_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $opts->sel = 't.*, t1.start AS __start, t1.end AS __end';
    $opts->jt = 'LEFT JOIN pb_colg_intake AS t1 ON t1.id = t.intake_id';
    $res = $orm->get('pb_colg_class', 't.name', 'ASC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function get_lang_classes($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.type = :v1 OR t.type = :v2';
    $opts->cdv = array(':v1' => PT_LANGUAGE, ':v2' => PT_NCEA_ENGLISH);
    $res = $orm->get('pb_colg_class', 't.name', 'ASC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function get_ncea_classes1($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.type = :v1 OR t.type = :v2';
    $opts->cdv = array(':v1' => PT_LANGUAGE, ':v2' => PT_NCEA_ENGLISH);
    $res = $orm->get('pb_colg_class', 't.name', 'ASC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function get_ncea_classes2($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.type = :v1 OR t.type = :v2 OR t.intake_id = :v3';
    $opts->cdv = array(':v1' => PT_LANGUAGE, ':v2' => PT_NCEA_ENGLISH, ':v3' => $req->id);
    $res = $orm->get('pb_colg_class', 't.name', 'ASC', NULL, NULL, $opts)->d;
    echo json_encode($res);
  }
  
  public static function submit_enrl_intakes($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    foreach ($req as $idx => $itm) {
      if ($idx == 0 && isset($itm->enrollment_id)) {
        $obj = new stdClass();
        $obj->id = $itm->enrollment_id;
        $obj->is_enrolled = 1;
        $orm->update('pb_colg_enrollment', $obj);
      }
      
      if (isset($itm->id) && $itm->id != 0) {
        $orm->update('pb_colg_enrolled_intake', $itm);
        $orm->exec('UPDATE pb_colg_enrolled_class SET start = \''. $itm->start . '\', end =\'' . $itm->end . '\' WHERE enrolled_intake_id = \'' . $itm->id . '\'');
      } else {
        $orm->add('pb_colg_enrolled_intake', $itm);
        $itm->id = $orm->last_id();
      }
    }
    
    echo json_encode($req);
    
  }
  
  public static function delete_enrl_intake($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $opts = new stdClass();
    $opts->sel = 'COUNT(t.id) AS num';
    $opts->cd = 't.enrolled_intake_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    if ($orm->get('pb_colg_enrolled_class', NULL, NULL, NULL, NULL, $opts)->d[0]->num > 0) {
      echo 'Delete All Classes First';
      return;
    }
    $orm->delete('pb_colg_enrolled_intake', $req);
  }
  
  public static function submit_enrl_class($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_colg_enrolled_class', $req);
    } else {
      $obj = new stdClass();
      $obj->id = $req->enrollment_id;
      $obj->is_enrolled = 1;
      $orm->update('pb_colg_enrollment', $obj);
      $orm->add('pb_colg_enrolled_class', $req);
      $req->id = $orm->last_id();
    }
    
    echo json_encode($req);
  }
  
  public static function submit_enrl_class2($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    if (isset($req->id) && $req->id != 0) {
      $orm->update('pb_colg_enrolled_class', $req);
    } else {
      $orm->add('pb_colg_enrolled_class', $req);
      $req->id = $orm->last_id();
    }
    
    echo json_encode($req);
  }
  
  public static function delete_enrl_class($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $orm->delete('pb_colg_enrolled_class', $req);
  }
  
  public static function delete_enrlprogram($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.enrolled_program_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res = $orm->get('pb_colg_enrollment', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($res) > 0) {
      echo 'Delete the enrollment first';
      return;
    }
    
    $opts = new stdClass();
    $opts->cd = 't.enrolled_program_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res = $orm->get('pb_colg_enrolled_intake', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($res) > 0) {
      echo 'Delete the intakes first';
      return;
    }
    
     $orm->delete('pb_colg_enrolled_program', $req);
  }
}
?>